﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Configuration;

namespace Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Pdf
{
    public class  ImpressumModule : BaseModule
    {
        public ImpressumModule(IMetaProvider overviewProvider, Document pdfDocument, PdfFonts fontsProvider, BookConfiguration cfg)
            : base(overviewProvider, pdfDocument, fontsProvider, cfg)
        {
            HierarchyNode rootNode = metaProvider.GetRootNode();


            // Title
            HierarchyNode nodeImpressum = rootNode.Nodes[0];
            PdfPTable table = CreateImpressumTable();
            Tables.Add(new PdfBookPTable(table));
            ImpressumTitle(table, string.Empty);
            ImpressumTitle(table, "Impressum");

            foreach (HierarchyNode node in nodeImpressum.Nodes)
            {
                ImpressumProperty(table, node.Id, node.Name);
            }
          // ImpressumResize(table);
        }

        /// <summary>
        /// Two columns table, as property key and value
        /// </summary>
        /// <returns>created table object</returns>
        protected PdfPTable CreateImpressumTable()
        {
            PdfPTable table = new PdfPTable(2);
            float[] widths = new float[] { 20f, 80f };
            table.SetWidths(widths);
            table.TotalWidth = TotalWidth;
            return table;
        }

        public void ImpressumResize(PdfPTable table)
        {
            float height = table.CalculateHeights(true);
            PdfPRow row = table.GetRow(0);
            float filler = doc.PageSize.Height - height - 20.0f - row.CalculateHeights() - doc.TopMargin - doc.BottomMargin;
            row.GetCells()[0].PaddingTop = filler;
            
        }


        protected void ImpressumTitle(PdfPTable table, string subTitle)
        {

            Phrase ph = new Phrase(subTitle, BookFonts.FontImpressumTitle);
            PdfPCell cell = new PdfPCell(ph);
            cell.Border = Rectangle.NO_BORDER;
            cell.PaddingBottom = 8f;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            table.AddCell(cell);
            cell = new PdfPCell(new Phrase(string.Empty, BookFonts.FontImpressumValue));
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

        }

        protected void ImpressumProperty(PdfPTable table, string subTitleProperty, string subTitleContent)
        {

            Phrase ph = new Phrase(subTitleProperty, BookFonts.FontImpressumConcept);
            PdfPCell cell = new PdfPCell(ph);
            cell.Border = Rectangle.NO_BORDER;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            //           cell.SetLeading(0.0f, 1.4f);

       //     cell.PaddingTop = 8f;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase(subTitleContent, BookFonts.FontImpressumValue));
            cell.Border = Rectangle.NO_BORDER;
 //           cell.HorizontalAlignment = Element.ALIGN_RIGHT;
 //           cell.SetLeading(0.0f, 1.4f);

            table.AddCell(cell);
        }

        /// <summary>Table representing Header of page </summary>
        /// <param name="title">title to put in header</param>
        /// <returns>the created table</returns>
        public static PdfPTable HeaderTable(string title, float totalWidth, PdfFonts fonts)
        {
            PdfPTable table = new PdfPTable(1);
            float[] widths = new float[] { 100f };
            table.SetWidths(widths);
            table.TotalWidth = totalWidth;

            // Only one cell with Title at top left
            Phrase p = new Phrase(title, fonts.FontImpressumValue);
            PdfPCell cell = new PdfPCell(p);
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            return table;
        }

        public static PdfPTable FooterTable(string title, int pageNr, float totalWidth, PdfFonts fonts)
        {
            PdfPTable table = new PdfPTable(2);
            float[] widths = new float[] { 50f, 50f };
            table.TotalWidth = totalWidth;

            // We invert text and page number according to page generated
            string left = pageNr % 2 == 0 ? pageNr.ToString() : title;
            string right = pageNr % 2 == 0 ? title : pageNr.ToString();
            Font leftFont = pageNr % 2 == 0 ? fonts.FontImpressumTitle : fonts.FontImpressumValue;
            Font rightFont = pageNr % 2 == 0 ? fonts.FontImpressumValue : fonts.FontImpressumTitle;

            // 1st cell is number if even, else user defined bottom text
            Phrase p = new Phrase(left, leftFont);
            PdfPCell cell = new PdfPCell(p);
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

            // 2nd cell is number if off, else user defined bottom text
            p = new Phrase(right, rightFont);
            cell = new PdfPCell(p);
            cell.Border = Rectangle.NO_BORDER;
            cell.HorizontalAlignment = Element.ALIGN_RIGHT;
            table.AddCell(cell);


            return table;
        }

    }
}
